﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VirtualPress.Portal.Biz;
using VirtualPress.Infrastructure.Entities.Entity;
using VirtualPress.Portal.WebLogic;
using VirtualPress.Tool.Framework;
using VirtualPress.Infrastructure.Entities.Enum;

namespace VirtualPress.Portal.Web.Areas.WorkSpace.Controllers
{
#if !DEBUG
    [ErrorHandle]
#endif
    [Authorize]
    [WorkSpace(UserRole.Member)]
    public class HomeController : Controller
    {
        //
        // GET: /WorkSpace//

        public ActionResult Index()
        {
            var user = new User().Find(HttpContext.User.Identity.Name);
            switch (user.SystemRole)
            { 
                case Infrastructure.Entities.Enum.UserRole.Admin:
                    string journal = HttpContext.Request.QueryString["journal"];
                    if (!String.IsNullOrEmpty(journal))
                    {
                        return Redirect("/workspace/console/index");
                    }
                    return Redirect("/workspace/press/index");
                case Infrastructure.Entities.Enum.UserRole.JounalsAdmin:
                    return Redirect("/workspace/console/index");
                case UserRole.MagazineAdmin:
                    return Redirect("/workspace/magazine/index");
                case Infrastructure.Entities.Enum.UserRole.Expert:
                    return Redirect("/workspace/expert/index");
                case UserRole.ConferenceAdmin:
                    return Redirect("/workspace/meeting/index");
                default:
                case Infrastructure.Entities.Enum.UserRole.Member:
                    return Redirect("/workspace/member/index");
            }
        }
        public void SWFPrepareUpload()
        {
            new UploadTool().SWFPrepareUpload();
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="c">来至哪个controller</param>
        /// <param name="a">来至哪个action</param>
        /// <param name="s">1:成功 0:失败</param>
        /// <returns></returns>
        public ActionResult ServerMessage(string c, string a, int s)
        {
            switch (c.ToLower())
            { 
                case "home":
                    break;
                case "magazine":
                    switch (a.ToLower())
                    {
                        case "baseinfoedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改期刊资料保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/baseinfo\">基本资料</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改期刊资料保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/baseinfo\">基本资料</a>";
                            }
                            break;
                        case "introdutionedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改期刊简介保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/introduction\">期刊简介</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改期刊简介保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/introduction\">期刊简介</a>";
                            }
                            break;
                        case "announcementedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "主办方简介保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/announcement\">征稿启事</a>";
                            }
                            else
                            {
                                ViewBag.Message = "主办方简介保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/announcement\">征稿启事</a>";
                            }
                            break;
                        case "retrievaledit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改检索信息保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/retrieval\">检索信息</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改检索信息保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/retrieval\">检索信息</a>";
                            }
                            break;
                        case "attentionedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "编委会信息保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/attention\">作者须知</a>";
                            }
                            else
                            {
                                ViewBag.Message = "编委会信息保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/attention\">作者须知</a>";
                            }
                            break;
                        case "addlinks":
                            if (s == 1)
                            {
                                ViewBag.Message = "添加期刊链接保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/Links\">期刊链接</a>";
                            }
                            else
                            {
                                ViewBag.Message = "添加期刊链接保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/Links\">期刊链接</a>";
                            }
                            break;
                        case "linkedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "编辑期刊链接保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/Links\">期刊链接</a>";
                            }
                            else
                            {
                                ViewBag.Message = "编辑期刊链接保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/Links\">期刊链接</a>";
                            }
                            break;
                        case "settings":
                            if (s == 1)
                            {
                                ViewBag.Message = "期刊设置保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/settings\">期刊设置</a>";
                            }
                            else
                            {
                                ViewBag.Message = "期刊设置保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/magazine/settings\">期刊设置</a>";
                            }
                            break;
                    }
                    break;
                case "console":
                    switch (a.ToLower())
                    { 
                        case "baseinfoedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改期刊资料保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/baseinfo\">基本资料</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改期刊资料保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/baseinfo\">基本资料</a>";
                            }
                            break;
                        case "introdutionedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改期刊简介保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/introduction\">期刊简介</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改期刊简介保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/introduction\">期刊简介</a>";
                            }
                            break;
                        case "announcementedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改征稿启事保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/announcement\">征稿启事</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改征稿启事保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/announcement\">征稿启事</a>";
                            }
                            break;
                        case "retrievaledit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改检索信息保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/retrieval\">检索信息</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改检索信息保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/retrieval\">检索信息</a>";
                            }
                            break;
                        case "attentionedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改作者须知保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/attention\">作者须知</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改作者须知保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/attention\">作者须知</a>";
                            }
                            break;
                        case "addlinks":
                            if (s == 1)
                            {
                                ViewBag.Message = "添加期刊链接保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/Links\">期刊链接</a>";
                            }
                            else
                            {
                                ViewBag.Message = "添加期刊链接保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/Links\">期刊链接</a>";
                            }
                            break;
                        case "linkedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "编辑期刊链接保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/Links\">期刊链接</a>";
                            }
                            else
                            {
                                ViewBag.Message = "编辑期刊链接保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/Links\">期刊链接</a>";
                            }
                            break;
                        case "settings":
                            if (s == 1)
                            {
                                ViewBag.Message = "期刊设置保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/settings\">期刊设置</a>";
                            }
                            else
                            {
                                ViewBag.Message = "期刊设置保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/settings\">期刊设置</a>";
                            }
                            break;
                        case "doinvite":
                            if (s == 1)
                            {
                                ViewBag.Message = "邀请函已发出。等待该用户确认后成为编委会成员";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/Counsellor\">人员管理</a>";
                            }
                            else
                            {
                                ViewBag.Message = "该专家已经是本期刊的编委会成员，请不要重复添加";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/Counsellor\">人员管理</a>";
                            }
                            break;
                        case "mailtoinvite":
                            ViewBag.Message = "邀请函已发出。等待该用户确认后成为编委会成员";
                            ViewBag.Link = "返回 <a href=\"/workspace/console/Counsellor\">人员管理</a>";
                            break;
                        case "cacneleditorial":
                            ViewBag.Message = "删除编委会成员成功";
                            ViewBag.Link = "返回 <a href=\"/workspace/console/Counsellor\">人员管理</a>";
                            break;
                        case "cacnelreviser":
                            ViewBag.Message = "删除初审成员成功";
                            ViewBag.Link = "返回 <a href=\"/workspace/console/Counsellor\">人员管理</a>";
                            break;
                        case "doreviserinvite":
                            if (s == 1)
                            {
                                ViewBag.Message = "邀请函已发出。等待该用户确认后成为初审员";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/Counsellor\">人员管理</a>";
                            }
                            else
                            {
                                ViewBag.Message = "该会员已经是本期刊的初审员，请不要重复添加";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/Counsellor\">人员管理</a>";
                            }
                            break;
                        case "mailtoinvitereviser":
                            ViewBag.Message = "邀请函已发出。等待该用户确认后成为初审员";
                            ViewBag.Link = "返回 <a href=\"/workspace/console/Counsellor\">人员管理</a>";
                            break;
                        case "newmanuscriptcheck":
                            if (s == 1)
                            {
                                ViewBag.Message = "初审通过，专审缴费通知邮件已发出！";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/article?group=0\">最新稿件</a>";
                            }
                            else
                            {
                                ViewBag.Message = "初审不通过！拒绝理由已通过邮件发给投稿人。";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/article?group=0\">最新稿件</a>";
                            }
                            break;
                        case "publishjournalitem":
                            if (s == 2)
                            {
                                ViewBag.Message = "您的期刊已经被停用，如有疑问请联系管理员！";
                                ViewBag.Link = "返回 <a href=\"/workspace/console/index\">期刊首页</a>";
                            }
                            break;
                    }
                    break;
                case "press":
                    switch (a.ToLower())
                    {
                        case "enablejournal":
                            if (s == 1)
                            {
                                ViewBag.Message = "期刊已启用";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/journalmanage\">期刊运营</a>";
                            }
                            else
                            {
                                ViewBag.Message = "期刊启用失败或该期刊不存在，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/journalmanage\">期刊运营</a>";
                            }
                            break;
                        case "disablejournal":
                            if (s == 1)
                            {
                                ViewBag.Message = "期刊已停用";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/index\">期刊运营</a>";
                            }
                            else
                            {
                                ViewBag.Message = "期刊停用失败或该期刊不存在，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/index\">期刊运营</a>";
                            }
                            break;
                        case "deletejournal":
                            if (s == 1)
                            {
                                ViewBag.Message = "期刊已删除";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/index\">期刊运营</a>";
                            }
                            else
                            {
                                ViewBag.Message = "期刊删除失败或该期刊不存在，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/index\">期刊运营</a>";
                            }
                            break;
                        case "pushone":
                            if (s == 1)
                            {
                                ViewBag.Message = "添加新期刊成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/index\">期刊运营</a>";
                            }
                            else
                            {
                                ViewBag.Message = "添加新期刊失败，期刊管理员已是会员！（注：期刊管理员必须为是非本站会员）";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/index\">期刊运营</a>";
                            }
                            break;
                        case "newmeeting":
                            if (s == 1)
                            {
                                ViewBag.Message = "添加会议成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/MeetingsApply\">会议管理</a>";
                            }
                            else
                            {
                                ViewBag.Message = "添加会议失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/MeetingsApply\">会议管理</a>";
                           
                            }
                            break;
                        case "edit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改期刊资料保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/index\">期刊运营</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改期刊资料保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/press/index\">期刊运营</a>";
                            }
                            break;
                        case "editformeeting":
                            if (s == 1) {
                                ViewBag.Message = "修改会议详情保存成功";
                                ViewBag.Link = "返回 <a href=\"/WorkSpace/Press/MeetingsApply\">会议管理</a>";
                            }
                            break;
                    }
                    break;
                case "meeting":
                    switch (a.ToLower())
                    {
                        case "contactus":
                              if (s == 1)
                            {
                                ViewBag.Message = "修改关于我们保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/contactus\">关于我们</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改关于我们保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/contactus\">关于我们</a>";
                            }
                            break;
                        case "attentionedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改作者须知保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/attention\">作者须知</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改作者须知保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/attention\">作者须知</a>";
                            }
                            break;
                        case "announcementedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改征稿启事保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/announcement\">征稿启事</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改征稿启事保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/announcement\">征稿启事</a>";
                            }
                            break;
                        case "introdutionedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改会议介绍保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/introduction\">期刊简介</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改会议介绍保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/introduction\">期刊简介</a>";
                            }
                            break;
                        case "baseinfoedit":
                            if (s == 1)
                            {
                                ViewBag.Message = "修改会议资料保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/baseinfo\">基本资料</a>";
                            }
                            else
                            {
                                ViewBag.Message = "修改会议资料保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/baseinfo\">基本资料</a>";
                            }
                            break;
                        case "settings":
                            if (s == 1)
                            {
                                ViewBag.Message = "会议设置保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/settings\">期刊设置</a>";
                            }
                            else
                            {
                                ViewBag.Message = "会议设置保存失败，请稍后重试";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/settings\">期刊设置</a>";
                            }
                            break;
                        case "doinvite":
                            if (s == 1)
                            {
                                ViewBag.Message = "邀请函已发出。等待该用户确认后成为编委会成员";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/Counsellor\">人员管理</a>";
                            }
                            else
                            {
                                ViewBag.Message = "该专家已经是本会议的编委会成员，请不要重复添加";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/Counsellor\">人员管理</a>";
                            }
                            break;
                        case "catalog":
                            if (s == 1)
                            {
                                ViewBag.Message = "保存成功";
                                ViewBag.Link = "返回 <a href=\"/workspace/meeting/catalog\">出版会议集</a>";
                            }
                            break;
                            
                    }
                    break;
                case "manuscripts":
                    break;
            }
            return View();
        }
        public ActionResult UserInfo()
        {
            return View();
        }
        [HttpPost]
        public ActionResult UserInfo(string photo, string nickname, string birthday, int gender, int areaCode, int location, string email, string phone, string qq)
        {
            if (StringPlus.AllNotNullOrEmpty(nickname, birthday, email, phone, qq))
            {
                if (UserBiz.ChangeInfo(HttpContext.User.Identity.Name, new UploadTool().SaveCustomerPhoto(photo), nickname, birthday, gender, areaCode, location, email, phone, qq) != null)
                {
                    ViewBag.Message = "修改资料成功";
                }
                else
                {
                    ViewBag.Message = "保存失败，请稍后重试";
                }
            }
            else
            {
                ViewBag.Message = "您的输入有误，请重新输入";
            }
            return View();
        }
        public ActionResult Secret()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Secret(string oldpwd, string newpwd, string confirm)
        {
            if (StringPlus.AllNotNullOrEmpty(oldpwd, newpwd, confirm))
            {
                if (newpwd == confirm)
                {
                    if (UserBiz.ChangePassword(HttpContext.User.Identity.Name, oldpwd, newpwd) != null)
                    {
                        ViewBag.Message = "密码修改成功";
                        return View();
                    }
                    else
                    {
                        ViewBag.Message = "原密码输入错误，请重新输入";
                        return View();
                    }
                }
                else
                {
                    ViewBag.Message = "两次密码输入不一致，请重新输入";
                    return View();
                }
            }
            else
            {
                ViewBag.Message = "输入有误，请重新输入";
                return View();

            }
        }

        public ActionResult Forbidden()
        {
            return View();
        }
    }
}
